<template>
	<view class="page_content">
		<view class="head_content">
			<view class="tabs_box">
				<u-tabs ref="uTabs" :list="contentList" :current="contentCurrent" font-size="28"
					@change="changeContent($event, 1)" :is-scroll="false" active-color="#EE7F33"
					inactive-color="#666666" bg-color="#FFFFFF"></u-tabs>
				<!-- 	<u-tabs :list="contentList" :current="contentCurrent" :activeStyle="tabsActiveStyle"
					:inactiveStyle="tabsInactiveStyle" lineColor="#EE7F33" @change="changeContent($event, 1)" /> -->
				<view class="search" @click="jumpView('/pages/video/search')">
					<u-icon name="search" color="#666" size="28"></u-icon>
				</view>
			</view>
		</view>
		<view class="main_content" v-if="contentList && contentList.length">
			<swiper style="height: 100%;width: 100%;" :skip-hidden-item-layout="true" :current="contentCurrent"
				@change="changeContent($event, 2)">
				<swiper-item style="height: 100%;width: 100%;" v-for="(item, index) in contentList" :key="item.id">
					<scroll-view style="height: 100%;width: 100%;" :scroll-y="true" :refresher-enabled="true"
						:refresher-threshold="100" :refresher-triggered="refreshStatus"
						@refresherrefresh="refreshHandle" @scrolltolower="bottomHandle" @scroll="scrollHandle">
						<view class="content_box">
							<view class="tabs" v-if="item.id == 0">
								<view class="item" :class="{ active: tabsActive.id == tabItem.id }"
									v-for="(tabItem, tabIndex) in tabsList" :key="tabIndex"
									@click="changeTabs(tabItem)">{{ tabItem.name }}</view>
							</view>
							<view class="list" v-if="item.id == 0">
								<block v-for="(lItem, lIndex) in item.list" :key="lIndex">
									<view class="sitem" v-if="lIndex % 10 == 0" @click="openVideoDetail(lItem.id)">
										<view class="left">
											<image class="image" :src="lItem.image" mode="aspectFill"></image>
										</view>
										<view class="right">
											<view class="info">
												<view class="tbox">
													<view class="title">{{ lItem.title }}</view>
													<view class="text u-line-5">{{ lItem.description }}</view>
												</view>
											</view>
											<view class="button" :class="{ collect: lItem.is_favorite == 1 }"
												hover-class="active" :hover-start-time="0" :hover-stay-time="200"
												@click.stop="handleCollect(lItem.id, lItem.is_favorite, lIndex)">
												<u-icon :name="lItem.is_favorite == 1 ? 'star-fill' : 'star'"
													color="#eee" size="30"></u-icon>
												<text class="text">{{ lItem.is_favorite == 1 ? '已追剧' : '追剧' }}</text>
											</view>
										</view>
									</view>
									<view class="item item1" v-else @click="openVideoDetail(lItem.id)">
										<view class="img">
											<image class="image" :src="lItem.image" mode="aspectFill"></image>
										</view>
										<view class="info">
											<view class="title u-line-1">{{ lItem.title }}</view>
											<view class="text u-line-1">{{ lItem.description }}</view>
										</view>
									</view>
								</block>
							</view>
							<view class="list" v-else>
								<view class="item item2" v-for="(lItem, lIndex) in item.list" :key="lIndex"
									@click="openVideoDetail(lItem.id)">
									<view class="img">
										<image class="image" :src="lItem.image" mode="aspectFill"></image>
									</view>
									<view class="info">
										<view class="title u-line-1">{{ lItem.title }}</view>
										<view class="text u-line-1">{{ lItem.description }}</view>
									</view>
								</view>
							</view>
							<view class="nodata" v-if="!item.list.length && item.status == 'nomore'">
								<u-empty mode="data" icon="http://cdn.uviewui.com/uview/empty/data.png" />
							</view>
							<view class="liststatus" v-else>
								<u-loadmore :status="item.status" :line="true" />
							</view>
						</view>
					</scroll-view>
				</swiper-item>
			</swiper>
		</view>
		<tabbar />
	</view>
</template>

<script>
	import {
		apiVideoCategoty,
		apiVideoLists,
	} from '@/api/store'
	export default {
		data() {
			return {
				navbarTitle: this.$store.state.app.title || '影视视频',
				tabsActiveStyle: {
					color: '#EE7F33',
					fontSize: '32rpx',
					fontWeight: 'bold',
				},
				tabsInactiveStyle: {
					color: '#666666'
				},
				contentList: [{
					id: 0,
					name: '精选短剧',
					list: [],
					page: 1,
					pagesize: 10,
					status: 'loadmore'
				}, ],
				contentCurrent: 0,
				refreshStatus: true,
				isRefresh: false,
				tabsList: [{
						id: 1,
						name: '推荐',
						type: 'recommend'
					},
					{
						id: 2,
						name: '最新',
						type: 'new'
					},
					{
						id: 3,
						name: '热门',
						type: 'hot'
					},
					{
						id: 4,
						name: '免费',
						type: 'free'
					},
				],
				tabsActive: {
					id: 1,
					name: '推荐',
					type: 'recommend'
				},
				isTabsFixed: false,
			}
		},
		computed: {

		},
		watch: {

		},
		onLoad() {
			this.getVideoClassify()
		},
		methods: {
			// 	收藏
			// handleCollect(id, collect, index) {
			// 	if (collect == 0) {
			// 		const obj = {
			// 			vid: id,
			// 			type: 'favorite'
			// 		}
			// 		this.$request('video.addRecord', obj, false).then(res => {
			// 			if (res.code === 1) {
			// 				this.contentList[this.contentCurrent].list[index].is_favorite = 1
			// 			}
			// 		})
			// 	} else {
			// 		const obj = {
			// 			ids: id,
			// 			type: 'favorite'
			// 		}
			// 		this.$request('video.deleteRecord', obj, false).then(res => {
			// 			if (res.code === 1) {
			// 				this.contentList[this.contentCurrent].list[index].is_favorite = 0
			// 			}
			// 		})
			// 	}
			// },
			// // 获取视频分类
			getVideoClassify() {
				apiVideoCategoty().then((res) => {
					res.lists.forEach((item, idenx) => {
						this.contentList.push({
							id: item.id,
							name: item.category_name,
							list: [],
							page: 1,
							pagesize: 10,
							status: 'loadmore'
						})
					})
					this.getVideoList()
				})
			},
			openVideoDetail(id) {
				this.$Router.push({
				  path: "/pages/short_play/detail",
				  query: { id },
				});
			},
			// 获取视频列表
			getVideoList() {
				const obj = {
					category_id: this.contentCurrent == 0 ? '' : this.contentList[this.contentCurrent].id,
					return_type: this.contentCurrent == 0 ? this.tabsActive.id : '',
					page_no: this.contentList[this.contentCurrent].page,
					page_size: this.contentList[this.contentCurrent].pagesize
				}
				this.contentList[this.contentCurrent].status = 'loading'
				apiVideoLists(obj).then((res) => {
					if (res.lists && res.lists.length) {
						this.contentList[this.contentCurrent].list = this.contentList[this.contentCurrent].list
							.concat(res.lists)
						this.contentList[this.contentCurrent].status = 'loadmore'
						if (res.lists.length < this.contentList[this.contentCurrent].pagesize) {
							this.contentList[this.contentCurrent].status = 'nomore'
						} else {
							this.contentList[this.contentCurrent].status = 'loadmore'
						}
					}else{
						this.contentList[this.contentCurrent].page > 1 && this.contentList[this.contentCurrent]
							.page--
						const timer = setTimeout(() => {
							this.contentList[this.contentCurrent].status = 'nomore'
							clearTimeout(timer)
						}, 500)
					}
					this.refreshStatus = false
					this.isRefresh = false
				})

			},
			// 下拉刷新
			refreshHandle() {
				this.refreshStatus = true
				if (!this.isRefresh) {
					this.isRefresh = true
					this.contentList[this.contentCurrent].page = 1
					this.contentList[this.contentCurrent].list = []
					this.getVideoList()
				}
			},
			// 滚动监听
			scrollHandle(e) {
				if (this.contentCurrent == 0) {
					this.isTabsFixed = e.detail.scrollTop > 200 ? true : false
				}
			},
			// 触底滚动
			bottomHandle() {
				this.contentList[this.contentCurrent].page++
			},
			// // 切换分类
			changeContent(e, i) {
				let current = i === 1 ? e : e.detail.current
				if (current == this.contentCurrent) return
				this.contentCurrent = current
				!this.contentList[this.contentCurrent].list.length && this.getVideoList()
			},
			// // 切换tabs
			changeTabs(item) {
				if (item.id == this.tabsActive.id) return
				this.tabsActive = item
				this.contentList[0].page = 1
				this.contentList[0].list = []
				this.getVideoList()
			}
		}
	}
</script>

<style lang="scss" scoped>
	uni-page-body {
		height: 100% !important;
	}

	.page_content {
		overflow: hidden;
		height: 100vh;
		// height: 100%;
		position: relative;
		display: flex;
		flex-direction: column;

		.tabs {
			padding-bottom: 30rpx;
			display: flex;
			align-items: center;

			&.top {
				height: 0;
				padding: 0 40rpx;
				overflow: hidden;
				transition: all 0.2s linear;

				&.show {
					height: 94rpx;
					padding: 20rpx 40rpx;
				}
			}

			.item {
				color: #666666;
				padding: 6rpx 26rpx;
				border-radius: 50rpx;
				font-size: 24rpx;
				font-weight: 700;

				&.active {
					color: #fff;
					background: #EE7F33;
				}
			}
		}

		.head_content {
			background-color: #FFF;
			.tabs_box {
				padding: 0 20rpx 10rpx;
				margin-right: 80rpx;
				position: relative;

				.search {
					position: absolute;
					top: 50%;
					right: 0;
					transform: translate(100%, -55%);
				}
			}
		}

		.main_content {
			flex: 1;
			overflow: hidden;
			.content_box {
				padding: 20rpx 40rpx 60rpx 40rpx;

				.list {
					display: flex;
					flex-wrap: wrap;

					.sitem {
						width: 100%;
						height: 516rpx;
						border-radius: 20rpx;
						overflow: hidden;
						display: flex;
						margin-bottom: 40rpx;

						.left {
							width: 52%;

							.image {
								width: 100%;
								height: 100%;
							}
						}

						.right {
							width: 48%;
							background: linear-gradient(0deg, rgba(35, 36, 40, 1) 0%, rgba(100, 105, 124, 1) 100%);
							padding: 30rpx;
							display: flex;
							flex-direction: column;
							justify-content: space-between;
							color: #fff;

							.info {
								height: 100%;
								display: flex;
								flex-direction: column;

								.tbox {
									flex: 1;
								}

								.title {
									font-size: 32rpx;
									font-weight: 700;
								}

								.text {
									font-size: 24rpx;
									margin-top: 16rpx;
									color: #eee;
									line-height: 40rpx;
								}

								.text1 {
									flex: 1;
								}
							}

							.button {
								height: 70rpx;
								display: flex;
								align-items: center;
								justify-content: center;
								font-size: 28rpx;
								background: #484B52;
								border-radius: 10rpx;
								margin-top: 16rpx;

								&.collect {
									opacity: 0.6;
								}

								&.active {
									background: rgba(#000, 0.2);
								}

								.text {
									margin-left: 8rpx;
								}
							}
						}
					}

					.item {
						width: calc((100% - 50rpx) / 3);
						margin-bottom: 40rpx;
						margin-right: 25rpx;

						&.item1 {

							&:nth-child(10n),
							&:nth-child(10n-3),
							&:nth-child(10n-6) {
								margin-right: 0;
							}
						}

						&.item2 {
							&:nth-child(3n) {
								margin-right: 0;
							}
						}

						.img {
							width: 100%;
							height: 280rpx;
							border-radius: 20rpx;
							overflow: hidden;

							.image {
								width: 100%;
								height: 100%;
							}
						}

						.info {
							margin-top: 16rpx;

							.title {
								font-size: 28rpx;
								color: #111;
								font-weight: 700;
							}

							.text {
								font-size: 24rpx;
								color: #666;
								margin-top: 4rpx;
							}
						}
					}
				}

				.nodata {
					padding: 15vh 0;
				}
			}
		}
	}
</style>